package com.icinfo.cloud.provider.punish.controller;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.punish.service.ICaseApprovalWaitRecordService;
import com.icinfo.cloud.provider.punish.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.service.IDocumentSendRecordService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.common.web.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 掌上办案首页信息交互控制器
 */
@RestController
@RequestMapping("app/home")
public class HomeController {

    @Resource
    private ICaseApprovalWaitRecordService caseApprovalWaitRecordService;
    @Resource
    private IDocumentSendRecordService documentSendRecordService;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Value("${member.url}")
    private String memberUrl;
    @Value("${spring.profiles.active}")
    private String env;
    /**
     *  获取用户登录信息
     *
     * @param
     * @return Result
     * @author lxj
     * @date 2022/2/23
     **/
    @RequestMapping("/getLoginUserInfo")
    public Result<UserDTO> getLoginUserInfo() {
        UserDTO userDTO = UserUtil.getUserInfo();
        if (userDTO == null) {
            return Result.success(450, "未登录或登录信息已失效");
        }
        return Result.success(userDTO);

    }

    /**
     * 通过dingId 获取登录用户token接口
     * @author  lxj
     *
     */
    @GetMapping("getLoginToken")
    public Result getLoginToken(@RequestParam String dingId){
        String token ="";
        String response = "";
        if(env.equals("dev")){
            response =  HttpUtil.createGet("http://192.168.1.200/api/v2/auth/users/getTokenByDingId?dingdingId=78012198").execute().body();
        }else{
            //根据渝快政id获取token
            response =  HttpUtil.createGet(memberUrl.concat("/usercenter/user/getTokenByDingId?dingdingId="+dingId)).execute().body();
        }
        System.out.println("-----------------"+response);
        JSONObject obj = JSONObject.parseObject(response);
        if("200".equals(obj.getString("code"))){
            token = obj .getString("data");
        }
        return Result.success("success",token);
    }

    /**
     * 上一次登录选择部门获取
     * @author  lxj
     *
     */
    @GetMapping("getLastDepartment")
    public Result getLastDepartment(@RequestParam(value = "userId") String userId){
        return Result.success("");
    }
    /**
     * 移动端获取待审批，送达数据
     * @author  lxj
     *
     */
    @GetMapping("getAuditTaskMsg")
    public Result getWaitHandleMsg(@RequestParam(value = "userId") String userId){

        //获取待审核案件数量
        int waitAuditSum = caseApprovalWaitRecordService.countWaitHandle(userId);
        //获取待送达数量
        int waitSendSum = documentSendRecordService.countWaitSendDocument(userId);
        //待办数量
        int waitTodoSum = caseMainInfoService.getCaseMainWaitTaskCount(userId);
        Map<String,Object> map = new HashMap<>(2);
        map.put("waitAuditSum",waitAuditSum);
        map.put("waitSendSum",waitSendSum);
        map.put("waitTodoSum",waitTodoSum);
        return Result.success(map);
    }
    
}
